package menu.lt.pers.menu;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.support.v4.widget.NestedScrollView;
import android.widget.TextView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;
import menu.lt.pers.menu.Bean.UserBean;
import menu.lt.pers.menu.adapter.RecipeByTimeRecyclerViewAdapter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Fragment_index extends Fragment {
    private Banner banner;                  //轮播图
    private List<Integer> images;           //图片资源列表
    private RecyclerView recyclerView;
    private RecipeByTimeRecyclerViewAdapter adapter;
    private TextView tv_title;
    private Boolean login_state = false;
    private UserBean user;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_index,container,false);
        init(view);
        setListener();
        return view;
    }

    private void setListener() {
//        scrollview 嵌套recycleview 滑动卡顿
//        scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
//            @Override
//            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//                View view = scrollView.getChildAt(scrollView.getChildCount()-1);
//                int d = view.getBottom();
//                d -= (scrollView.getHeight()+scrollView.getScrollY());
//                Log.i("setListener", "onScrollChange: "+d);
//                if(d==0) {
//                    adapter.addList();
//                }
//            }
//        });
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    private void init(View view) {
        //banner初始化
        banner = view.findViewById(R.id.banner_home);
        recyclerView = view.findViewById(R.id.recyclerView);
        tv_title = view.findViewById(R.id.tv_title);
        Bundle bundle = getArguments();
        if (bundle!=null){
            login_state = bundle.getBoolean("loginstate");
            user = bundle.getParcelable("user");
        }
        switch(getClassid()){
            case 562 :
                tv_title.setText(getResources().getString(R.string.breakfast));
                break;
            case 563:
                tv_title.setText(getResources().getString(R.string.launch));
                break;
            case 564:
                tv_title.setText(getResources().getString(R.string.tea));
                break;
            case 565:
                tv_title.setText(getResources().getString(R.string.wanfan));
                break;
            case 566:
                tv_title.setText(getResources().getString(R.string.yexiao));
                break;
            default :
        }
        images = new ArrayList<>();
        images.add(R.drawable.banner1);
        images.add(R.drawable.banner2);
        images.add(R.drawable.banner3);

        if (banner != null){
            banner.setImages(images);                               //设置图片集
            banner.setDelayTime(3000);                              //图片轮播事件间隔
            banner.setImageLoader(new Fragment_index.GlideImageLoader());          //设置图片加载器
            banner.setIndicatorGravity(BannerConfig.CENTER);      //设置banner样式
            banner.start();
        }
        GridLayoutManager manager = new GridLayoutManager(getActivity(),2){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        manager.setSmoothScrollbarEnabled(false);
        recyclerView.addItemDecoration(new SpaceItemDecoration(10));
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        adapter = new RecipeByTimeRecyclerViewAdapter(getActivity(),getClassid(),user);
        recyclerView.setAdapter(adapter);
    }
    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            imageView.setImageResource((Integer) path);
        }
        @Override
        public ImageView createImageView(Context context) {
            return new ImageView(context);
        }
    }


    public Activity fragment_activity;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.fragment_activity = (Activity)context;
    }
    //根据当前时间判断所处时间段，给用户推荐食谱
    public int getClassid(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (hour<10 && hour>5)
            return 562;
        else if (hour>=10 && hour<13)
            return 563;
        else if (hour>=13 && hour<17)
            return 564;
        else if (hour>=17 && hour<19)
            return 565;
        else if (hour>=19 && hour<24)
            return 566;
        else
            return 573;
    }

    public class SpaceItemDecoration extends RecyclerView.ItemDecoration {

        private int space;
        public SpaceItemDecoration(int space) {
            this.space = space;
        }
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left = space;
            outRect.bottom = space;
            if (parent.getChildLayoutPosition(view) %2==0) {
                outRect.left = 0;
            }
        }

    }
}
